package com.lanyou.cook.entity.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;
import com.lanyou.cook.datadictionary.ScheduleRunResult;
import com.lanyou.cook.service.DataDictionaryService;

@Entity
@EntityListeners({ AuditingEntityListener.class })
@Table(name = "T_COOK_BU_SKD_RUN_RESULT")
public class InterfaceScheduleResult {

	private String id;
	private int version;

	private String scheduleId;
	// 调度开始时间
	private Date scheduleTime;
	// 调度结果
	private String scheduleResult;
	// 这些是冗余字段
	private String fieldName;
	private String fieldSystemName;
	private String interfaceName;
	private String scheduleName;
	
	private String creator;
	private String modifier;
	private Date createTime;
	private Date modifyTime;

	public final static Specification<InterfaceScheduleResult> EmptySpecification = new Specification<InterfaceScheduleResult>(){
		@Override
		public Predicate toPredicate(Root<InterfaceScheduleResult> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			return null;
		}
	};
	
	public static Specification<InterfaceScheduleResult> idSpecification(String id){
		return new Specification<InterfaceScheduleResult>(){
			@Override
			public Predicate toPredicate(Root<InterfaceScheduleResult> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("id"), id));
				return null;
			}
		};
	}
	
	
	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "SKD_RUN_RESULT_ID", length = 36)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	@Version
	@JsonIgnore
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}
	
	@Column(name = "JOB_SCHEDULE_ID")
	public String getScheduleId() {
		return scheduleId;
	}

	public void setScheduleId(String scheduleId) {
		this.scheduleId = scheduleId;
	}
	
	@Column(name = "SKD_RESULT", length = 10)
	public String getScheduleResult() {
		return scheduleResult;
	}

	public void setScheduleResult(String scheduleResult) {
		this.scheduleResult = scheduleResult;
	}

	@Column(name = "SKD_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	public Date getScheduleTime() {
		return scheduleTime;
	}

	public void setScheduleTime(Date scheduleTime) {
		this.scheduleTime = scheduleTime;
	}

	@Column(name = "FIELD_NAME")
	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	@Column(name = "FIELD_SYSTEM_NAME")
	public String getFieldSystemName() {
		return fieldSystemName;
	}

	public void setFieldSystemName(String fieldSystemName) {
		this.fieldSystemName = fieldSystemName;
	}

	@Column(name = "INTERFACE_NAME")
	public String getInterfaceName() {
		return interfaceName;
	}

	public void setInterfaceName(String interfaceName) {
		this.interfaceName = interfaceName;
	}

	@Column(name = "SCHEDULE_NAME")
	public String getScheduleName() {
		return scheduleName;
	}

	public void setScheduleName(String scheduleName) {
		this.scheduleName = scheduleName;
	}
	
	@CreatedBy
	@Column(length = 50, updatable = false)
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@LastModifiedBy
	@Column(length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "CREATED_DATE", updatable = false)
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@CreatedDate
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "LAST_UPDATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@LastModifiedDate
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	@Transient
	public String getScheduleResultForDisplay() {
		return DataDictionaryService.getInstance().getValueName(ScheduleRunResult.TypeCode, scheduleResult);
	}

	@Transient
	public String getScheduleTimeForExcel() {
		if(scheduleTime!=null){
			return scheduleTime.toString();
		}
		return "";
	}
	
}
